import Axios, { AxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from 'axios'

import settle from 'axios/lib/core/settle'
import buildURL from 'axios/lib/helpers/buildURL'

import { useUserStore } from '@/store/modules/user'
import { getProductIdMap } from '@/utils/common';

// #ifdef H5
const baseURL: string = import.meta.env.VITE_API_BASE_URL || ''
// #endif
// #ifdef MP-WEIXIN || MP-QQ
import { apiHost } from '../../config'

const baseURL: string = apiHost.endsWith('/') ? apiHost.substring(0, apiHost.length - 1) : apiHost
// #endif

// 临时使用localStorage存储移动端productId到同应用pc端productId的映射
// getProductIdMap().then(res => {
//     uni.setStorageSync('productIdMap', JSON.stringify(res));
// });

const axios = Axios.create({
    // baseURL:'http://10.20.0.35:8089', // 基础 url
    baseURL, // 基础 url
    withCredentials: true,
    timeout: 10000, // 超时 10s
    headers: {
        'X-Requested-With': 'XMLHttpRequest'
    }
})

axios.defaults.adapter = function (config) {
    return new Promise((resolve, reject) => {
        uni.request({
            method: config.method.toUpperCase(),
            url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {
                handleUnauthorized(response.statusCode)
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                }
                settle(resolve, reject, response)
            }
        })
    })
}

axios.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        // console.log('Request is about to be sent:', config);
        // 附加上token和uid
        const userStore = useUserStore()
        // 预发环境
        // const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyVXVpZCI6IjA2MThmMWI5LWJiZmItNDQyZS04NTgyLTM2OGE2NTZlZjlkYSIsImV4cCI6MTcyMzk2MjQ3NCwidXNlcklkIjoiMTAwNjUwIiwidGltZXN0YW1wIjoiMTcxODc3ODQ3NDM5NyJ9.HyoDXNC4d9u0lcD0Czrp3o4jH44IuqPB6NraY9RFiog'
        // const uid = '0618f1b9-bbfb-442e-8582-368a656ef9da'
        // const productId = 21
        // const Tenantid = 2
        // const current_user_id = 100650

        // 测试环境
        const token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyVXVpZCI6IjQxMTgxZTdmLTI3ZGUtNDJjOS1hMjY1LWFhNDk2NDYyZWQ4MCIsImV4cCI6MTc2ODk2MTIzNSwidXNlcklkIjoiMTAwMzMwIiwidGltZXN0YW1wIjoiMTc2Mzc3NzIzNTc4NiJ9.Jnd5UzE5A0MhUWT0OelwzXcz1rSJsyqmsUCycStJfDs'
        const uid = '41181e7f-27de-42c9-a265-aa496462ed80'
        const productId = 1
        const Tenantid = 2
        const current_user_id = 100330


        
        // 张梦洁
        // const uid = '9e9f3136-d186-4727-865f-3b282b9fec47'
        // const current_user_name = '%u51AF%u817E%u98DE'

        if (!config.headers) {
            config.headers = {} as AxiosRequestHeaders
        }
        if (token) {
            config.headers['token'] = token
            if (uid) {
                config.headers['uid'] = uid
            }
           
            // config.headers['current_user_name'] = current_user_name
        }
      
        // 临时默认处理移动端productId到同应用pc端productId的映射
        // config.headers['productid'] = 7


        // config.headers['tenantid'] = 100406
        const currentUserName = '5Yav6IW+6aOe'

        config.headers['appId'] = 'product-container'
        config.headers['productId'] = productId
        config.headers['Tenantid'] = Tenantid
        config.headers['current_user_id'] = current_user_id
        config.headers['current_user_name'] = currentUserName
        // config.headers['current_user_id'] = 100309
        // config.headers['current_user_name'] = '6ams5Yab'

        // config.headers['current_user_id'] = 100650
        // config.headers['current_user_name'] = '5Yav6IW+6aOe'

        config.headers['production'] = 'DPAs'
        config.headers['timestamp'] = new Date().getTime()
        return config
    },
    (error: any) => {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(
    response => {
        // 服务器正常返回
        const data = response.data
        if (response?.config?.responseType) {
            if (['arraybuffer', 'blob'].includes(response.config.responseType)) {
                return response
            }
        }
        // 判断http状态码为2xx
        if (response.status >= 200 && response.status < 300) {
            return response
        } else if (response.status === 401) {
            handleUnauthorized(response.status)
            return Promise.reject(response)
        } else {
            // 其他错误，直接抛出错误
            return Promise.reject(data.errorCode)
        }
    },
    error => {
        return Promise.reject({
            code: error.response.status,
            message: error.response.statusText
        })
    }
)

const handleUnauthorized = statusCode => {
    // if (statusCode === 401) {
    //     const userStore = useUserStore()
    //     userStore.logout()
    //     uni.showToast({
    //       title: '登录过期，请重新登录',
    //       icon: 'none'
    //     })
    //     uni.reLaunch({
    //       url: '/pages/Login/Login'
    //     })
    // }
}
export default axios

export function get<T = any, D = any>(
    url: string,
    params?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    const localConfig = config ? config : ({} as AxiosRequestConfig<D>)
    if (params) {
        localConfig.params = Object.assign({}, localConfig.params ? localConfig.params : {}, params)
    }
    return axios.get<HttpResponse<T>>(url, localConfig).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}

export function getWithErrorMessage<T = any, D = any>(
    url: string,
    params?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    const localConfig = config ? config : ({} as AxiosRequestConfig<D>)
    if (params) {
        localConfig.params = Object.assign({}, localConfig.params ? localConfig.params : {}, params)
    }
    return axios
        .get<HttpResponse<T>>(url, localConfig)
        .then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
        .then(resp => {
            if (resp.status !== 'success') {
                return Promise.reject(resp.errorMsg)
            }
            return resp
        })
}

export function post<T = any, D = any>(
    url: string,
    data?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    return axios.post<HttpResponse<T>>(url, data, config).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}

export function postWithErrorMessage<T = any, D = any>(
    url: string,
    data?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    return axios
        .post<HttpResponse<T>>(url, data, config)
        .then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
        .then(resp => {
            if (resp.status !== 'success') {
                return Promise.reject(resp.errorMsg)
            }
            return resp
        })
}

export function put<T = any, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<HttpResponse<T>> {
    return axios.put<HttpResponse<T>>(url, data, config).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}
export function putWithErrorMessage<T = any, D = any>(
    url: string,
    data?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    return axios
        .put<HttpResponse<T>>(url, data, config)
        .then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
        .then(resp => {
            if (resp.status !== 'success') {
                return Promise.reject(resp.errorMsg)
            }
            return resp
        })
}

export function patch<T = any, D = any>(
    url: string,
    data?: D,
    config?: AxiosRequestConfig<D>
): Promise<HttpResponse<T>> {
    return axios.patch<HttpResponse<T>>(url, data, config).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}

export function del<T = any, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<HttpResponse<T>> {
    return axios.delete<HttpResponse<T>>(url, config).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}

// TODO add upload and download

export function request<T = any, D = any>(config: AxiosRequestConfig<D>): Promise<HttpResponse<T>> {
    return axios.request<HttpResponse<T>>(config).then((resp: AxiosResponse<HttpResponse<T>>) => resp.data)
}

export function requestWithHeaders<T = any, D = any>(config: AxiosRequestConfig<D>): Promise<HttpResponse<T>> {
    return axios.request<HttpResponse<T>>(config).then((resp: AxiosResponse<HttpResponse<T>>) => resp)
}
